package com.yuexun.dhdoctor.module.contractmanage.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;


import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.BaseEntity;
import com.yuexun.dhdoctor.base.BaseKJActivity;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.contractmanage.adapter.OutPatientAdviceAdapter;
import com.yuexun.dhdoctor.module.contractmanage.adapter.RecordOutpatientInspectAdapter;
import com.yuexun.dhdoctor.module.contractmanage.adapter.RecordOutpatientTestAdapter;
import com.yuexun.dhdoctor.module.contractmanage.bean.InspectBean;
import com.yuexun.dhdoctor.module.contractmanage.bean.OutPatientAdviceBean;
import com.yuexun.dhdoctor.module.contractmanage.bean.OutPatientRecordBean;
import com.yuexun.dhdoctor.module.contractmanage.bean.TestBean;
import com.yuexun.dhdoctor.module.contractmanage.model.impl.ContractManageModel;
import com.yuexun.dhdoctor.module.contractmanage.presenter.impl.InspectPresenter;
import com.yuexun.dhdoctor.module.contractmanage.presenter.impl.OutPatientAdvicePresenter;
import com.yuexun.dhdoctor.module.contractmanage.presenter.impl.TestPresenter;
import com.yuexun.dhdoctor.module.contractmanage.ui.view.IInspectView;
import com.yuexun.dhdoctor.module.contractmanage.ui.view.IOutPatientAdviceView;
import com.yuexun.dhdoctor.module.contractmanage.ui.view.ITestView;
import com.yuexun.dhdoctor.widget.FixedSwipeListView;

import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by work-jx on 2016/4/13.
 */
public class Act_RecordOutpatinet extends BaseKJActivity implements IInspectView,ITestView,IOutPatientAdviceView{
    @Bind(R.id.title_tv)
    TextView titleTv;
    @Bind(R.id.recordoutpatient_date)
    TextView recordoutpatientDate;
    @Bind(R.id.recordoutpatient_visitno)
    TextView recordoutpatientVisitno;
    @Bind(R.id.recordoutpatient_hospital_name)
    TextView recordoutpatientHospitalName;
    @Bind(R.id.recordoutpatient_type)
    TextView recordoutpatientType;
    @Bind(R.id.recordoutpatient_diagnosis)
    TextView recordoutpatientDiagnosis;
    @Bind(R.id.recordoutpatient_doctor_name)
    TextView recordoutpatientDoctorName;
    @Bind(R.id.recordoutpatient_department)
    TextView recordoutpatientDepartment;
    @Bind(R.id.recordoutpatient_icd)
    TextView recordoutpatientIcd;
    @Bind(R.id.recordoutpatient_disease)
    TextView recordoutpatientDisease;
    @Bind(R.id.record_inspect_list)
    FixedSwipeListView recordInspectList;
    @Bind(R.id.record_test_list)
    FixedSwipeListView recordTestList;
    @Bind(R.id.record_advice_list)
    FixedSwipeListView recordAdviceList;

    private BaseAdapter adapter_test;
    private BaseAdapter adapter_inspect;
    private BaseAdapter adapter_advice;

    private InspectPresenter presenter_inspect;
    private TestPresenter presenter_test;
    private OutPatientAdvicePresenter presenter;

    private OutPatientRecordBean.RowsBean outPatientRecordBean = new OutPatientRecordBean.RowsBean();

    List<InspectBean.RowsBean> checkDataList = new ArrayList<>();
    List<TestBean.RowsBean> chemicalDataList = new ArrayList<>();
    List<OutPatientAdviceBean.RowsBean> adviceDataList = new ArrayList<>();
    String name;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_recordoutpatient);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        setTitle();
        outPatientRecordBean = (OutPatientRecordBean.RowsBean) getIntent().getExtras().get("OutPatientRecordBean");
        name = getIntent().getExtras().getString("name");
        setContent();
        presenter = new OutPatientAdvicePresenter(this,new ContractManageModel());
        presenter_inspect = new InspectPresenter(this,new ContractManageModel());
        presenter_test = new TestPresenter(this,new ContractManageModel());

        recordInspectList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putString("hosId", checkDataList.get(position).getHosp_id());
                bundle.putString("applyNo", checkDataList.get(position).getApply_no());
                Intent intent = new Intent(aty, Act_ContractManageInspectDetail.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        recordTestList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putString("hosId", chemicalDataList.get(position).getHosp_id());
                bundle.putString("rpeportNo", chemicalDataList.get(position).getReport_no());
                Intent intent = new Intent(aty, Act_ContractManageTestDetail.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        ApiForGetList_Inspect();
        ApiForGetList_Test();
        ApiForGetList_Advice();
    }

    private void ApiForGetList_Inspect() {
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        params.put("pageIndex", 1+"");
        params.put("pageSize", 100+"");
        params.put("sourceNo", outPatientRecordBean.getOutp_no());
        params.put("hospId", outPatientRecordBean.getHosp_id());
//        params.put("sourceId", outPatientRecordBean.getOutp_no());
        presenter_inspect.showInspectDate(params);
    }

    private void ApiForGetList_Test() {
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        params.put("pageIndex", 1+"");
        params.put("pageSize", 100+"");
//        params.put("personId", outPatientRecordBean.getPerson_id());
        params.put("hospId", outPatientRecordBean.getHosp_id());
        params.put("sourceNo", outPatientRecordBean.getOutp_no());
        presenter_test.showTestDate(params);
    }

    private void ApiForGetList_Advice() {
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        params.put("pageIndex", 1+"");
        params.put("pageSize", 100+"");
        params.put("hospId", outPatientRecordBean.getHosp_id());
        params.put("outpNo", outPatientRecordBean.getOutp_no());
        presenter.showOutPatientAdviceDate(params);
    }

    private void setContent() {
        recordoutpatientDate.setText("就诊日期  " + outPatientRecordBean.getCli_dtime());
        recordoutpatientVisitno.setText("门诊号  " + outPatientRecordBean.getOutp_no());
        recordoutpatientHospitalName.setText(outPatientRecordBean.getHosp_name());
        recordoutpatientType.setText("门诊");
//        recordoutpatientDiagnosis.setText(outPatientRecordBean.getDiag_name());
        recordoutpatientDoctorName.setText(outPatientRecordBean.getCli_doct_name());
        recordoutpatientDepartment.setText(outPatientRecordBean.getCli_dept_name());
        switch (outPatientRecordBean.getDiag_encoding_type()) {
            case "1":
                recordoutpatientIcd.setText("是");
                break;
            case "2":
                recordoutpatientIcd.setText("否");
                break;
            case "3":
                recordoutpatientIcd.setText("-");
                break;
            case "9":
                recordoutpatientIcd.setText("不详");
                break;
            default:
                recordoutpatientIcd.setText("");
                break;
        }
        recordoutpatientDisease.setText(outPatientRecordBean.getDiag_name());
    }


    @OnClick(R.id.back)
    public void onClick() {
        finish();
    }

    protected void setTitle() {
        titleTv.setText("门诊记录");
    }

    @Override
    public void showTestDate(BaseEntity<TestBean> list) {
        chemicalDataList = list.getDatas().get(0).getRows();
        adapter_test = new RecordOutpatientTestAdapter(recordTestList, chemicalDataList, R.layout.recordhospital_item);
        recordTestList.setAdapter(adapter_test);
    }

    @Override
    public void showInspectDate(BaseEntity<InspectBean> list) {
        checkDataList = list.getDatas().get(0).getRows();
        adapter_inspect = new RecordOutpatientInspectAdapter(recordInspectList, checkDataList, R.layout.recordhospital_item);
        recordInspectList.setAdapter(adapter_inspect);
    }

    @Override
    public void showOutPatientAdviceDate(BaseEntity<OutPatientAdviceBean> list) {
        adviceDataList = list.getDatas().get(0).getRows();
        adapter_advice = new OutPatientAdviceAdapter(recordAdviceList, adviceDataList, R.layout.item_outpatient_advice);
        recordAdviceList.setAdapter(adapter_advice);
    }
}
